library(stargazer)
library(foreign)
setwd("~/Replication Code/")

##---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")

gss0610 <- read.dta("./data/gss0610_subset.dta")

naes <- read.dta("./data/naes_subset.dta")

#----------------------------------------------------------------------------------------------#
## GSS stereotypes
dat1 <- na.omit(gss0610[c("whtintvw_1", "whtintvw_3",
                           "pid_1_sc", "pid_3_sc", "wtpannr123",
                           "bwdif_stype_1_scaled", "bwdif_stype_3_scaled")])

## PID on Stereotypes
m1 <- lm(bwdif_stype_3_scaled ~  pid_1_sc  + bwdif_stype_1_scaled, 
         data = dat1, weights = wtpannr123,
         subset = whtintvw_1 == whtintvw_3)
summary(m1)
## Stereotypes on PID
m10 <- lm(pid_3_sc ~  pid_1_sc  + bwdif_stype_1_scaled, 
          data = dat1, weights = wtpannr123, 
          subset = whtintvw_1 == whtintvw_3)
summary(m10)

#----------------------------------------------------------------------------------------------#
## NAES
dat2 <- na.omit(naes[c("pid7_3", "pid7_5", "WT_35", "bw_stype_tot_3", "bw_stype_tot_5")])

# Box-Cox transformation
dat2$t_1 <- (dat2$bw_stype_tot_3^.18-1)/.18
dat2$t_2 <- (dat2$bw_stype_tot_5^.18-1)/.18

### PID on Stypes
m2 <- lm(t_2 ~  pid7_3  + t_1, 
           data = dat2, weights = WT_35)
summary(m2)

### Stypes on PID
m20 <- lm(pid7_5 ~  pid7_3  + t_1, 
            data = dat2, weights = WT_35)
summary(m20)

#----------------------------------------------------------------------------------------------#
## GSS Intermarriage
dat3 <- na.omit(gss0610[c("whtintvw_1", "whtintvw_3",
                           "pid_1_sc", "pid_3_sc", "wtpannr123",
                           "inmar_pref_1", "inmar_pref_3")])
# PID on Intermmarriage
m3 <- lm(inmar_pref_3 ~  pid_1_sc  + inmar_pref_1, 
         data = dat3, weights = wtpannr123,
         subset = whtintvw_1 == whtintvw_3)
summary(m3)

# Intermarriage on PID
m30 <- lm(pid_3_sc ~  pid_1_sc  + inmar_pref_1, 
          data = dat3, weights = wtpannr123, 
          subset = whtintvw_1 == whtintvw_3)
summary(m30)

## Table
m1_p <- m1
m10_p <- m10
m2_p <- m2
m20_p <- m20

names(m1_p$coefficients) <- names(coef(m1_p))
names(m10_p$coefficients) <- names(coef(m1_p))
names(m2_p$coefficients) <- names(coef(m1_p))
names(m20_p$coefficients) <- names(coef(m1_p))

### Table 1 
stargazer(m1_p, m10_p, m2_p, m20_p, m3, m30, title = "Relationship between Whites' Partisanship and Racial Attitudes",
          covariate.labels = c("Partisanship$_{t-1}$", 
                               "Anti-Black Stereotypes$_{t-1}$",
                               "Same-Race Marriage Preference$_{t-1}$"),
          dep.var.labels = c("Anti-Black Stereotypes", "Partisanship",
                             "Anti-Black Stereotypes", "Partisanship",
                             "Same-Race Marriage Preference", "Partisanship"), 
          no.space = T, notes = c("OLS regression results. Robust standard errors in parentheses.",
                                  "Variables scale 0-1.",
                                  "Data from the 2006-2010 GSS panel and 2008 NAES panels. Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T,
          digits = 3, omit.stat = c("f", "adj.rsq"), df = F, header = F,
          align = T, star.char = c("*"), star.cutoffs = c(0.05)
)

#----------------------------------------------------------------------------------------------#
# Affect
#----------------------------------------------------------------------------------------------------#
# ANES 1992-1994 -----
dat1 <- na.omit(anes9297[c("pid92_7scaled", "pid94_7scaled", 
                            "V940005",
                            "white_int92","white_int94",
                            "ft_wht92_sc", "ft_wht94_sc",
                            "ft_blk92_sc", "ft_blk94_sc")])
dat1$ft_blk92_sc_rev <- dat1$ft_blk92_sc*-1 + 1
dat1$ft_blk94_sc_rev <- dat1$ft_blk94_sc*-1 + 1

### Blacks Only
# PID on Feeling Thermometers
m1.1_blk <- lm(ft_blk94_sc_rev ~  pid92_7scaled + ft_blk92_sc_rev,
               data = dat1, weights = V940005,
               subset = white_int92 == white_int94)
summary(m1.1_blk)

# FT on PID
m1.10_blk <- lm(pid94_7scaled ~  pid92_7scaled  + ft_blk92_sc_rev, data = dat1, weights = V940005,
                subset = white_int92 == white_int94)
summary(m1.10_blk)

### Whites Only
# PID on Feeling Thermometers
m1.1_wht <- lm(ft_wht94_sc ~  pid92_7scaled + ft_wht92_sc,
               data = dat1, weights = V940005,
               subset = white_int92 == white_int94)
summary(m1.1_wht)

# FT on PID
m1.10_wht <- lm(pid94_7scaled ~  pid92_7scaled  + ft_wht92_sc, data = dat1, weights = V940005,
                subset = white_int92 == white_int94)
summary(m1.10_wht)


#----------------------------------------------------------------------------------------------------#
# VSG 2012-2016 -----
#----------------------------------------------------------------------------------------------------#
dat2 <- na.omit(vsg.dat[c("pid7_sc_12", "pid7_sc_16", "weight", "blacks_ft_sc_12", "blacks_ft_sc_16",
                           "whites_ft_sc_12", "whites_ft_sc_16")])
dat2$blacks_ft_sc_12_rev <- dat2$blacks_ft_sc_12*-1 + 1
dat2$blacks_ft_sc_16_rev <- dat2$blacks_ft_sc_16*-1 + 1

### Black FT
# PID on affect
m2_blk <- lm(blacks_ft_sc_16_rev ~ pid7_sc_12 + blacks_ft_sc_12_rev, 
             data = dat2, weights = weight)
summary(m2_blk)

# affect on PID
m20_blk <- lm(pid7_sc_16 ~ pid7_sc_12 + blacks_ft_sc_12_rev, 
              data = dat2, weights = weight)
summary(m20_blk)


### White FT
# PID on affect
m2_wht <- lm(whites_ft_sc_16 ~ pid7_sc_12 + whites_ft_sc_12, 
             data = dat2, weights = weight)
summary(m2_wht)
# affect on PID
m20_wht <- lm(pid7_sc_16 ~ pid7_sc_12 + whites_ft_sc_12, 
              data = dat2, weights = weight)
summary(m20_wht)

#----------------------------------------------------------------------------------------------------#
# CCAP 2016 -----
#----------------------------------------------------------------------------------------------------#
dat3 <- na.omit(ccap16[c("b_pid7_sc", "p_pid7_sc",  "weight_post", "b_fav_blk_sc", "p_fav_blk_sc",  "b_fav_wht_sc", "p_fav_wht_sc")])
dat3$b_fav_blk_sc_rev <- dat3$b_fav_blk_sc*-1 + 1
dat3$p_fav_blk_sc_rev <- dat3$p_fav_blk_sc*-1 + 1

##### Blacks Only
# PID on Fav
m3_blk <- lm(p_fav_blk_sc_rev ~ b_pid7_sc + b_fav_blk_sc_rev, 
             data = dat3, weights = weight_post)
summary(m3_blk)

# Fav on PID
m30_blk <- lm(p_pid7_sc ~ b_pid7_sc + b_fav_blk_sc_rev, 
              data = dat3, weights = weight_post)
summary(m30_blk)

##### Whites Only
# PID on Fav
m3_wht <- lm(p_fav_wht_sc ~ b_pid7_sc + b_fav_wht_sc, 
             data = dat3, weights = weight_post)
summary(m3_wht)

# Fav on PID
m30_wht <- lm(p_pid7_sc ~ b_pid7_sc + b_fav_wht_sc, 
              data = dat3, weights = weight_post)
summary(m30_wht)

#----------------------------------------------------------------------------------------------------#
# Tables
#----------------------------------------------------------------------------------------------------#
# Whites only
m1.1_wht_p <-  m1.1_wht
m1.10_wht_p <- m1.10_wht
m2_wht_p <- m2_wht
m20_wht_p <-m20_wht
m3_wht_p <- m3_wht
m30_wht_p <- m30_wht

names(m1.1_wht_p$coefficients) <- names(coef(m1.1_wht))
names(m1.10_wht_p$coefficients) <- names(coef(m1.1_wht))
names(m2_wht_p$coefficients) <- names(coef(m1.1_wht))
names(m20_wht_p$coefficients) <- names(coef(m1.1_wht))
names(m3_wht_p$coefficients) <- names(coef(m1.1_wht))
names(m30_wht_p$coefficients) <- names(coef(m1.1_wht))

stargazer(m1.1_wht_p, m1.10_wht_p, m2_wht_p, m20_wht_p, m3_wht_p, m30_wht_p, 
          title = "Relationship between Partisanship and White Affect",
          covariate.labels = c("Partisanship$_{t-1}$", "White Affect$_{t-1}$"),
          dep.var.labels = c("White Affect", "Partisanship", "White Affect", "Partisanship", "White Affect", "Partisanship"),
          column.labels = c("ANES 1992-1994", "VOTER Survey 2012-2016", "CCAP 2016"),
          column.separate = c(2, 2, 2),
          dep.var.caption = "", model.numbers = F,
          no.space = T, notes = c("OLS regression results. Standard errors in parentheses. Variables scaled 0-1.",
                                  "Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T, 
          df = F, header = F,
          digits = 3, omit.stat = c("f", "adj.rsq"), align = T, star.char = c("*"), star.cutoffs = c(0.05)
)

# Blacks only
m1.1_blk_p <-  m1.1_blk
m1.10_blk_p <- m1.10_blk
m2_blk_p <- m2_blk
m20_blk_p <-m20_blk
m3_blk_p <- m3_blk
m30_blk_p <- m30_blk

names(m1.1_blk_p$coefficients) <- names(coef(m1.1_blk))
names(m1.10_blk_p$coefficients) <- names(coef(m1.1_blk))
names(m2_blk_p$coefficients) <- names(coef(m1.1_blk))
names(m20_blk_p$coefficients) <- names(coef(m1.1_blk))
names(m3_blk_p$coefficients) <- names(coef(m1.1_blk))
names(m30_blk_p$coefficients) <- names(coef(m1.1_blk))

stargazer(m1.1_blk_p, m1.10_blk_p, m2_blk_p, m20_blk_p, m3_blk_p, m30_blk_p, 
          title = "Relationship between Partisanship and Negative Black Affect",
          covariate.labels = c("Partisanship$_{t-1}$", "Negative Black Affect$_{t-1}$"),
          dep.var.labels = c("Negative Black Affect", "Partisanship", "Negative Black Affect", "Partisanship", "Negative Black Affect", "Partisanship"),
          column.labels = c("ANES 1992-1994", "VOTER Survey 2012-2016", "CCAP 2016"),
          column.separate = c(2, 2, 2),
          dep.var.caption = "", model.numbers = F,
          no.space = T, notes = c("OLS regression results. Standard errors in parentheses. Variables scaled 0-1.",
                                  "Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T, 
          df = F, header = F,
          digits = 3, omit.stat = c("f", "adj.rsq"), align = T, star.char = c("*"), star.cutoffs = c(0.05)
)


